import java.util.LinkedList;
import java.util.Scanner;

/*
 * ****************************
 * Eric Doherty
 * Jesse Kinney
 * 
 * TCSS 422 Scheduler simulator
 * Main Application Class
 * 
 * 6/4/08
 * ****************************
 */

public class MainApp
{
  public static void main(String[] args)
  {
    CPU cpu;
    IO io1 = new IO();
    IO io2 = new IO();
    LinkedList<Process> processes = new LinkedList<Process>();
    Scanner scan = new Scanner(System.in);
    System.out.println("TCSS 422 Scheduler simulator");
    System.out.print("Total Run Time?>");
    int totalRunTime = 0;
    while (totalRunTime == 0)
    {
      try
      {
        totalRunTime = scan.nextInt();
      }
      catch (NumberFormatException e)
      {
        totalRunTime = 10;
        System.out.println("Run time will default to 10");
      }
    }
    if (totalRunTime <= 0)
    {
      totalRunTime = 10;
      System.out.println("Run time will default to 10");

    }
    System.out.print("How many processes?>");
    int numProcesses = 0;
    while (numProcesses == 0)
    {
      try
      {
        numProcesses = scan.nextInt();
      }
      catch (NumberFormatException e)
      {
        numProcesses = 5;
        System.out.println("Process count will default to 5");
      }
    }
    if (numProcesses <= 0)
    {
      numProcesses = 5;
      System.out.println("Process count will default to 5");

    }
    System.out.println();
    System.out.println("1 or 2: Generate Producer/Consumer Pair");
    System.out.println("3: Generate Interactive Process");
    System.out.println("4: Generate Standard Process");
    System.out.println("5: Generate Calculating Process");
    for (int i = 0; i < numProcesses; i++)
    {
      int index = 0;
      System.out.println("Process " + (i + 1) + " will be:");
      while (index == 0)
      {
        try
        {
          index = scan.nextInt();
        }
        catch (NumberFormatException e)
        {
          index = 4;
          System.out.println("Default to 4 (normal process)");
        }
      }
      switch (index)
      {
        case 1:

        case 2:
          i++;
          MemoryLocation m = new MemoryLocation();
          processes.add(new Producer(m));
          processes.add(new Consumer(m));
          System.out.println("Producer/Consumer Pair Generated");
          break;

        case 3:
          processes.add(new InteractiveProcess());
          System.out.println("Interactive Process Generated");
          break;

        default:

        case 4:
          processes.add(new Process());
          System.out.println("General Process Generated");
          break;
        case 5:
          processes.add(new CalculatingProcess());
          System.out.println("Calculating Process Generated");
          break;

      }
    }
    System.out.println("Your processes are:");
    int j = 1;
    System.out.println("**********************");
    for (Process p : processes)
    {
      p.addObserver(io1);
      p.addObserver(io2);
      System.out.println(j + ": " + p);
      j++;
    }
    System.out.println("**********************");
    System.out.print("Is this okay?(y/n)>");
    String s = scan.next();
    System.out.println();
    switch (s.toLowerCase().toCharArray()[0])
    {
      case 'n':
        System.out.println("Have a nice day");
        System.exit(0);
        break;
      default:
        System.out
            .println("I didn't understand that, so I'm going to go ahead and start...");
      case 'y':
        System.out.println("Starting simulation...");
        cpu = new CPU(processes, totalRunTime);
        io1.addObserver(cpu);
        io2.addObserver(cpu);
        cpu.loop();
        if (io1.getTimer() != null)
        {
          io1.getTimer().stop();
        }
        if (io2.getTimer() != null)
        {
          io2.getTimer().stop();
        }
        break;
    }
    System.out.println("Simulation Ending");
    System.exit( 0 );
  }
}
